package com.tedu.demo;


public class Test1_Exercise {
    public static void main(String[] args) {
        MyRunnable target = new MyRunnable();
        Thread thread1 = new Thread(target, "线程一");
        Thread thread2 = new Thread(target, "线程二");
        Thread thread3 = new Thread(target, "线程三");
        Thread thread4 = new Thread(target, "线程四");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
    }
}
class MyRunnable implements Runnable {
    int tic = 100;
// 方法二:   Object object = new Object();
    @Override
// 方法一:   synchronized public void run() {
    public void run() {
        while (true){
            //方法二: synchronized(object) {
            synchronized(MyRunnable.class) {  //方法三:含有静态资源时只能用方法三
                if (tic > 0) {
                    try {
                        Thread.sleep(2);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "票号" + tic--);
                } else {
                    break;
                }
            }
        }
    }
}
